package cerebrum.common;

import java.io.File;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 壁纸加载器
 */
public class ImagesDataLoaderUtil {
    //壁纸状态
    private static volatile Boolean taskStatus = true;
    //壁纸缓存
    private static List<String> imagesUrl = new ArrayList<>();

    //屏保状态
    private static volatile Boolean screenTaskStatus = false;

    private static Random random;

    static {
        try {
            random = SecureRandom.getInstanceStrong();
            imagesLoader();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    /**
     * 加载壁纸
     */
    private static void imagesLoader() {
        File file = new File(FilePathUtil.getWallpaperImagesPath());
        File[] files = file.listFiles();
        for (File imageFile : files) {
            imagesUrl.add(imageFile.toURI().toString());
        }
    }
    //获取一张壁纸url
    public static synchronized String next() {
        return imagesUrl.get(random.nextInt(imagesUrl.size()));
    }

    //重新加载壁纸
    public static synchronized void beReborn() {
        imagesUrl.removeAll(imagesUrl);
        imagesLoader();
    }

    /**
     * 获取壁纸线程状态
     */
    public static Boolean getTaskStatus() {
        return taskStatus;
    }

    /**
     * 设置壁纸状态
     */
    public static void setTaskStatus(Boolean taskStatus) {
        ImagesDataLoaderUtil.taskStatus = taskStatus;
    }


    public static Boolean getScreenTaskStatus() {
        return screenTaskStatus;
    }

    public static void setScreenTaskStatus(Boolean screenTaskStatus) {
        ImagesDataLoaderUtil.screenTaskStatus = screenTaskStatus;
    }
}
